package com.atguigu.serviceedu.service.impl;

import com.atguigu.serviceedu.client.VodClient;
import com.atguigu.serviceedu.entity.EduVideo;
import com.atguigu.serviceedu.mapper.EduVideoMapper;
import com.atguigu.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    //诸如删除视频
    @Autowired
    private VodClient vodClient;

    //根据课程id删除
    @Override
    @Transactional
    public void deleteVideoByCourseId(String courseId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();

        wrapper.eq("course_id", courseId);

        //删除小节前删除所有的视频
        //查询课程号式courseid的小节有哪些，每个都要删除
        List<EduVideo> eduVideos = baseMapper.selectList(wrapper);
        if(eduVideos!=null&&eduVideos.size()!=0){
            for (EduVideo eduVideo:eduVideos) {
                String videoSourceId = eduVideo.getVideoSourceId();
                if(!StringUtils.isEmpty(videoSourceId)){
                    vodClient.removeVideo(videoSourceId);
                }
            }
        }
        baseMapper.delete(wrapper);
    }

}
